from typing import Optional
from typing_extensions import Annotated, TypedDict

# 使用TypedDict定义模式
class StudentInfo(TypedDict):
    """学生基本信息"""
    
    name: Annotated[str, ..., "学生姓名"]
    age: Annotated[int, ..., "学生年龄"]
    major: Annotated[str, ..., "所学专业"]
    score: Annotated[Optional[float], None, "平均成绩，0-100分"]

from langchain_ollama import ChatOllama
llm = ChatOllama(model="qwen3:8b", temperature=0.5, reasoning=False)

structured_llm = llm.with_structured_output(StudentInfo)

result = structured_llm.invoke("张明，20岁，计算机科学专业，平均成绩85分")
print(result)
# 输出: {'name': '张明', 'age': 20, 'major': '计算机科学', 'score': 85.0}